package com.czl.module_storehouse.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.czl.base.data.bean.tengyun.BorrowBean;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.fragment.BaseRecyclerViewFragment;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_service.event.BorrowHomeEvent;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.borrow.add.BorrowAddActivity;
import com.czl.module_storehouse.activity.borrow.details.BorrowDetailsActivity;
import com.czl.module_storehouse.activity.borrow.details.BorrowDetailsPostActivity;
import com.czl.module_storehouse.adapter.BorrowAdapter;
import com.czl.module_storehouse.databinding.CommonButtonBinding;
import com.czl.module_storehouse.databinding.FragmentReceiveBinding;
import com.czl.module_storehouse.event.BorrowEvent;
import com.czl.module_storehouse.mvp.presenter.BorrowPresenter;
import com.czl.module_storehouse.mvp.view.BorrowView;
import com.jeremyliao.liveeventbus.LiveEventBus;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

/**
 * @author by xubin,
 * @date on 2022/2/10.
 * PS: Not easy to write code, please indicate.
 */
public class BorrowFragment extends BaseRecyclerViewFragment<FragmentReceiveBinding> implements BorrowView {
    private BorrowAdapter mAdapter;
    private int mState;

    @InjectPresenter
    BorrowPresenter mBorrowPresenter;
    private boolean loading;

    public static BorrowFragment newInstance(int state) {
        Bundle args = new Bundle();
        args.putInt("state", state);
        BorrowFragment fragment = new BorrowFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected FragmentReceiveBinding getBinding(LayoutInflater layoutInflater, ViewGroup container) {
        return null;
    }

    @Override
    protected void reload() {
        super.reload();
        loading = true;
        loadData(true);
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        loadData(false);
    }

    private void loadData(boolean refresh) {
        if (mState == 0) {
            if (mBorrowPresenter != null) {
                mBorrowPresenter.getNeedDealtBorrowList(refresh);
            }
        } else {
            if (mBorrowPresenter != null) {
                mBorrowPresenter.getDealtBorrowList(refresh);
            }
        }
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            mState = arguments.getInt("state");
        }
        if (mState == 0) {
            CommonButtonBinding buttonBinding = CommonButtonBinding.inflate(getLayoutInflater(), mRecyclerViewBinding.containerBottom, true);
            buttonBinding.button.setText("添加借用单");
            buttonBinding.button.setOnClickListener(v -> startActivity(new Intent(requireContext(), BorrowAddActivity.class)));
        }
        mAdapter = new BorrowAdapter(R.layout.item_borrow, new ArrayList<>());
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (R.id.tv_borrow_total == view.getId()) {
                    getDetailsActivity(mAdapter.getItem(position));
                } else if (R.id.cl_root == view.getId()) {
                    goInfoActivity(mAdapter.getItem(position));
                }
            }

            private void getDetailsActivity(BorrowBean item) {
                BorrowEvent event = new BorrowEvent(item);
                EventBus.getDefault().postSticky(event);
                Intent intent = new Intent(requireContext(), BorrowDetailsActivity.class);
                startActivity(intent);
            }

            private void goInfoActivity(BorrowBean bean) {
                Intent intent = new Intent();
                BorrowEvent event = new BorrowEvent(bean);
                EventBus.getDefault().postSticky(event);
                if (mState == 1) {
                    intent.setClass(requireContext(), BorrowDetailsActivity.class);
                } else {
                    intent.setClass(requireContext(), BorrowDetailsPostActivity.class);
                }
                startActivity(intent);
            }
        });
        mAdapter.setVisible(mState == 0);
        mRecyclerViewBinding.recyclerView.setAdapter(mAdapter);

        LiveEventBus.get(BorrowHomeEvent.class).observeSticky(this, event -> {
            if (event != null && !loading) {
                reload();
            }
        });
    }

    @Override
    public void hideDialogLoading() {
        super.hideDialogLoading();
        loading = false;
    }

    @Override
    public <T> void showData(T t) {
        super.showData(t);
        if (t instanceof ListBean) {
            handleResponseData(mAdapter, mBorrowPresenter.getPageNo(), (ListBean<BorrowBean>) t);
        }
    }
}
